/*****************************************************************
 * Author: DO HOANG HAI
 * Date of creation: 3rd/Jan/2008
 * Last modification: 25/Jan/2008
 * 
 * Description: Provide means to obtain and manage the Buddy data
 * from the file buddy.xml in the database
 * 
 * Status: Optimized and Finished
 ****************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Pbdms.DataAccess.DataProvider
{
    /// <summary>
    /// a buddy provider manage performing transactions to database (read/write)
    /// </summary>
    public class BuddyProvider
    {
        const string FILENAME = "buddy.xml";
        /// <summary>
        /// create a BuddyProvider object
        /// </summary>
        /// <returns></returns>
        public static BuddyProvider create()
        {
            return new BuddyProvider();
        }
        /// <summary>
        /// get the list of all buddies from the database
        /// </summary>
        /// <returns></returns>
        public ArrayList getListOfBuddies()
        {
            ArrayList arlResult = new ArrayList();
        
                
                ArrayList arlName = XmlHelper.create("buddy.xml").selectMultipleElementValues("/root/buddy/name");
                ArrayList arlkeyId = XmlHelper.create("buddy.xml").selectMultipleElementValues("/root/buddy/keyId");
                ArrayList arlId = XmlHelper.create("buddy.xml").selectMultipleAttributeValues("/root/buddy", "id");

                int index = 0;
                if (arlId != null && arlId.Count > 0)
                {
                    foreach (object id in arlId)
                    {
                        Common.DataObject.Buddy bdd = new Pbdms.Common.DataObject.Buddy(int.Parse(arlId[index].ToString()), arlName[index].ToString(), arlkeyId[index].ToString());
                        arlResult.Add(bdd);
                        index++;
                    }
                }
                
         

            return arlResult;
        }
        /// <summary>
        /// update a buddy to database
        /// </summary>
        /// <param name="bdd"></param>
        public void updateBuddyToDB(Common.DataObject.Buddy bdd)
        {

            XmlHelper.create(FILENAME).updateTypedElement("/root/buddy[@id='" + bdd.Id + "']" + "/name", bdd.Name);
            XmlHelper.create(FILENAME).updateTypedElement("/root/buddy[@id='" + bdd.Id + "']" + "/keyId", bdd.KeyID);

        }

        /// <summary>
        /// append a new buddy to the database
        /// </summary>
        /// <param name="bdd"></param>
        public void appendBuddyToDB(Common.DataObject.Buddy bdd)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<buddy id='" + bdd.Id + "'>");
            sb.Append("<name>"+ bdd.Name+"</name>");
            sb.Append("<keyId>" + bdd.KeyID + "</keyId>");
            sb.Append("</buddy>");
            XmlHelper.create(FILENAME).appendChild("/root",sb.ToString());
        }
        /// <summary>
        /// delete a buddy from the database
        /// </summary>
        /// <param name="bdd"></param>
        public void deleteBuddyFromDB(Common.DataObject.Buddy bdd)
        {
            XmlHelper.create(FILENAME).deleteSelf("/root/buddy[@id='" + bdd.Id + "']");
        }
        //return a buddy  id based on name
        public string selectIDFromDB(string name)
        {
            
          
            string temp = XmlHelper.create(FILENAME).getParentAttribute("/root/buddy/name[.  ='" + name + "']");
            return temp;
        }
        //return a buddy id based on keyid
        public string selectIDBasedOnKeyIDFromDB(string keyid)
        {


            string temp = XmlHelper.create(FILENAME).getParentAttribute("/root/buddy/keyId[.  ='" + keyid + "']");
            return temp;
        }
        //return a buddy based on on buddy id
        public Common.DataObject.Buddy selectABuddyBasedOnIDFromDB(string id)
        {
            Common.DataObject.Buddy buddy = new Pbdms.Common.DataObject.Buddy();
            buddy.Id = int.Parse(id);
            buddy.Name = XmlHelper.create(FILENAME).getInnerXML("/root/buddy[@id='" + id + "']/name");
            buddy.KeyID = XmlHelper.create(FILENAME).getInnerXML("/root/buddy[@id='" + id + "']/keyId");
            return buddy;
        }
        public Common.DataObject.Buddy selectABuddyBasedOnKeyIDFromDB(string keyID)
        {
            string temp = XmlHelper.create(FILENAME).getParentAttribute("/root/buddy/keyId[.  ='" + keyID + "']");
            if (temp != null)
            {
                Common.DataObject.Buddy buddy = new Pbdms.Common.DataObject.Buddy();
                buddy.Id = int.Parse(temp);
                buddy.Name = XmlHelper.create(FILENAME).getInnerXML("/root/buddy[@id='" + temp + "']/name");
                buddy.KeyID = XmlHelper.create(FILENAME).getInnerXML("/root/buddy[@id='" + temp + "']/keyId");
                return buddy;
            }
            else
            {
                return null;
            }
        }
    }
}
